package com.xcn.lang;


import java.security.*;
import java.text.*;

import com.xcn.exception.*;

public class StringUtils
{
	//private final Logger LOGGER = LoggerFactory.getLogger(KerberosWebHDFSConnection.class);
	
	
	public static void main ( String[] args ) throws NoSuchAlgorithmException
	{
		System.out.println ( encodeSha256 ( "Mulgama!@" ) );
	}
	
	
	/**
	 * 문자열을 SHA-256으로 변환해준다. 16진수는 소문자로 표기
	 * @param buffer
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static String encodeSha256 ( String buffer ) throws NoSuchAlgorithmException
	{
		if ( (buffer != null) && (buffer.length ( ) > 0) )
		{
			MessageDigest md = MessageDigest.getInstance ( "SHA-256" );
			md.reset ( );
			byte byteData[] = md.digest ( buffer.getBytes ( ) );
			
			StringBuffer sb = new StringBuffer ( );
			int cntData = byteData.length;
			for ( int i = 0 ; i < cntData ; i++ )
			{
				sb.append ( Integer.toString ( (byteData[i] & (0xff)) + 0x100, 16 ).substring ( 1 ) );
			}
			return String.valueOf ( sb );
		}
		else
		{
			return "";
		}
	}
	
	
	/**
	 * Java String isEmpty
	 * This Java String isEmpty shows how to check whether the given
	 * string is empty or not using isEmpty method of Java String class.
	 * @param target
	 * @return
	 */
	public static boolean isEmpty ( Object target )
	{
		return objFixNull ( target ).isEmpty ( );
	}

	/**
	 * Null to Empty String
	 * @param target
	 * @return
	 */
	public static String objFixNull ( Object target )
	{
		String result = "";
		if ( target != null )
		{
			if ( !String.valueOf ( target ).equals ( "null" ) ) return String.valueOf ( target );
		}
		return result;
	}
	
	/**
	 * Null to Empty String
	 * @param target
	 * @return
	 */
	public static int objFixZero ( Object target )
	{
		int result = 0;
		if ( target != null )
		{
			if ( !String.valueOf ( target ).equals ( "null" ) && !String.valueOf ( target ).equals ( "" ) ) return Integer.parseInt ( String.valueOf ( target ) );
		}
		return result;
	}
	
	/**
	 * 특정 문자열의 자릿수 만큼 원하는 문자열을 오른쪽에 채워 넣는다.
	 * @param nValue
	 * @param nLength
	 * @param nDefault
	 * @return
	 */
	public static String formatString ( String nValue, int nLength, String nDefault )
	{
		String result = nValue;
		if ( nValue.length ( ) < nLength )
		{
			int i = nLength - nValue.length ( );
			for ( int j = 0 ; j < i ; j++ )
			{
				result += nDefault;
			}
		}
		return result;
	}
	
	/**
	 * ip 문자열을 long값으로 변환
	 * 
	 * @param addr
	 * @return
	 * @throws Exception
	 */
	public static Long inet_aton ( String addr ) throws Exception
	{
		if ( addr.equals ( "" ) ) return 0L;
		
		String[] addrArray = addr.split ( "\\." );
		long num = 0;
		try
		{
			int cntAddr = addrArray.length;
			for ( int i = 0 ; i < cntAddr ; i++ )
			{
				int power = 3 - i;
				num += ((Integer.parseInt ( addrArray[i] ) % 256 * Math.pow ( 256, power )));
			}
		}
		catch ( Exception e )
		{
			e.printStackTrace ( );
		}
		return num;
	}
	
	/**
	 * lpad 함수
	 *
	 * @param str   대상문자열, len 길이, addStr 대체문자
	 * @return      문자열
	 */
	public static String lpad ( String str, int len, String addStr )
	{
		String result = str;
		int templen = len - result.length ( );
		for ( int i = 0 ; i < templen ; i++ )
		{
			result = addStr + result;
		}
		return result;
	}
	

	/**
	 * 타입 숫자에 세자리마다 콤마(,)로 숫자에 붙여 다시 String 으로 반환
	 * @param num
	 * @return
	 */
	public static String money ( Object num )
	{
		NumberFormat format = NumberFormat.getInstance ( );
		return format.format ( num );
	}

	
	/**
	 * 
	 * @param amt
	 * @return
	 * @throws Exception 
	 */
	public static String moneyToHangul ( String amt ) throws XCNException
	{
		if ( amt == null || amt.length ( ) == 0 )
			throw new XCNException ( "com.xcn.exception NullPointerException" );

		if ( amt.length ( ) >= 16 )
			throw new XCNException ( "The input data is too large." );

		String tmpamt = "";
		amt = "000000000000000" + amt;
		int j = 0;
		for ( int i = amt.length ( ) ; i > 0 ; i-- )
		{
			j++;
			if ( !amt.substring ( i - 1, i ).equals ( "0" ) )
			{
				if ( j % 4 == 2 ) tmpamt = "십" + tmpamt;
				if ( j % 4 == 3 ) tmpamt = "백" + tmpamt;
				if ( j > 1 && (j % 4) == 0 ) tmpamt = "천" + tmpamt;
			}
			if ( j == 5 && Integer.parseInt ( amt.substring ( amt.length ( ) - 8, amt.length ( ) - 4 ) ) > 0 ) tmpamt = "만" + tmpamt;
			if ( j == 9 && Integer.parseInt ( amt.substring ( amt.length ( ) - 12, amt.length ( ) - 8 ) ) > 0 ) tmpamt = "억" + tmpamt;
			if ( j == 13 && Integer.parseInt ( amt.substring ( amt.length ( ) - 16, amt.length ( ) - 12 ) ) > 0 ) tmpamt = "조" + tmpamt;
			if ( amt.substring ( i - 1, i ).equals ( "1" ) ) tmpamt = "일" + tmpamt;
			if ( amt.substring ( i - 1, i ).equals ( "2" ) ) tmpamt = "이" + tmpamt;
			if ( amt.substring ( i - 1, i ).equals ( "3" ) ) tmpamt = "삼" + tmpamt;
			if ( amt.substring ( i - 1, i ).equals ( "4" ) ) tmpamt = "사" + tmpamt;
			if ( amt.substring ( i - 1, i ).equals ( "5" ) ) tmpamt = "오" + tmpamt;
			if ( amt.substring ( i - 1, i ).equals ( "6" ) ) tmpamt = "육" + tmpamt;
			if ( amt.substring ( i - 1, i ).equals ( "7" ) ) tmpamt = "칠" + tmpamt;
			if ( amt.substring ( i - 1, i ).equals ( "8" ) ) tmpamt = "팔" + tmpamt;
			if ( amt.substring ( i - 1, i ).equals ( "9" ) ) tmpamt = "구" + tmpamt;
		}
		tmpamt = "금" + tmpamt + "원정";
		return tmpamt;
	}
}
